package model.dap.dao;

import java.util.ArrayList;

import model.dap.domain.Deal;
import model.dap.mapper.DealMapper;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import common.base.DaoConfig;

public class DealDAO implements DealMapper{
	SqlSession session  = DaoConfig.getSqlSessionFactory().openSession();
	Logger log = Logger.getLogger(DealDAO.class);
	// =====================================================================================================
	//VIEW DEAL LIST (ALL)
	@Override
	public ArrayList<Deal> searchForDeal(int limit, int offset) throws Exception{
		SqlSession session  = DaoConfig.getSqlSessionFactory().openSession();
		try {
			return session.getMapper(DealMapper.class).searchForDeal(limit, offset);
		}catch (Exception e) {
			session.rollback();
			throw e;
		}finally{
			session.close();
		}
	}
	
	// =====================================================================================================

	// COUNT NUMBER OF DEAL
	@Override
	public int countForSearch() throws Exception {
		SqlSession session  = DaoConfig.getSqlSessionFactory().openSession();
		try {
			return session.getMapper(DealMapper.class).countForSearch();
		}catch (Exception e) {
			session.rollback();
			throw e;
		}finally{
			session.close();
		}
	}
	// =====================================================================================================
	//VIEW DEAL LIST BY DEAL CATEGORY AND CITY
	@Override
	public ArrayList<Deal> searchForDealByCategoryAndCity(int limit, int offset, int cityID, int categoryID) throws Exception {
		// TODO Auto-generated method stub
		SqlSession session  = DaoConfig.getSqlSessionFactory().openSession();
		try {
			return session.getMapper(DealMapper.class).searchForDealByCategoryAndCity(limit, offset, cityID, categoryID);
		}catch (Exception e) {
			session.rollback();
			throw e;
		}finally{
			session.close();
		}
	}
	// =====================================================================================================
	//VIEW DEAL LIST BY CITY	
	@Override
	public ArrayList<Deal> searchForDealByCity(int limit, int offset, int cityID) throws Exception {
		// TODO Auto-generated method stub
		SqlSession session  = DaoConfig.getSqlSessionFactory().openSession();
		try {
			return session.getMapper(DealMapper.class).searchForDealByCity(limit, offset, cityID);
		}catch (Exception e) {
			session.rollback();
			throw e;
		}finally{
			session.close();
		}
	}

	
	@Override
	public Deal getDealbyID(int dealID) {
		try {
			return session.getMapper(DealMapper.class).getDealbyID(dealID);
		} catch (Exception e) {
			// TODO: handle exception
			session.rollback();
		} finally {
			session.close();
		}

		return null;
	}
	@Override
	public int updateNumberOrderDeal(Deal deal) {
		// TODO Auto-generated method stub
		try {
			return session.getMapper(DealMapper.class)
					.updateNumberOrderDeal(deal);
		} catch (Exception e) {
			log.error(e.getMessage());
			session.rollback();
		} finally {
			session.commit();
			session.close();
		}
		return 0;
	}
	
	@Override
	public Deal selectDeal(int dealID) {
		SqlSession session = DaoConfig.getSqlSessionFactory().openSession();
		// TODO Auto-generated method stub
		try {
			return session.getMapper(DealMapper.class)
					.selectDeal(dealID);
		} catch (Exception e) {
			log.error(e.getMessage());
			session.rollback();
		} finally {
			session.commit();
			session.close();
		}
		return null;
	}
	
	@Override
	public int updateEditNumberOrderDeal(Deal deal) {
		SqlSession session = DaoConfig.getSqlSessionFactory().openSession();
		// TODO Auto-generated method stub
		try {
			return session.getMapper(DealMapper.class)
					.updateEditNumberOrderDeal(deal);
		} catch (Exception e) {
			log.error(e.getMessage());
			session.rollback();
		} finally {
			session.commit();
			session.close();
		}
		return 0;
	}

	@Override
	public String selectCategoryName(int categoryID){
		// TODO Auto-generated method stub
		SqlSession session  = DaoConfig.getSqlSessionFactory().openSession();
		try {
			return session.getMapper(DealMapper.class).selectCategoryName(categoryID);
		} catch (Exception e) {
			log.error(e.getMessage());
			session.rollback();
		} finally {
			session.commit();
			session.close();
		}
		return null;
	}

	@Override
	public int countForSearchByCity(int cityID) throws Exception {
		// TODO Auto-generated method stub
		SqlSession session  = DaoConfig.getSqlSessionFactory().openSession();
		try {
			return session.getMapper(DealMapper.class).countForSearchByCity(cityID);
		}catch (Exception e) {
			session.rollback();
			throw e;
		}finally{
			session.close();
		}
	}

	@Override
	public int countForSearchByCityAndCategory(int cityID, int categoryID)
			throws Exception {
		// TODO Auto-generated method stub
		SqlSession session  = DaoConfig.getSqlSessionFactory().openSession();
		try {
			return session.getMapper(DealMapper.class).countForSearchByCityAndCategory(cityID, categoryID);
		}catch (Exception e) {
			session.rollback();
			throw e;
		}finally{
			session.close();
		}
	}
}
